/*
 * @Description: 动态路由
 * @Author: Ronda
 * @Date: 2021-03-26 10:01:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-04-27 03:49:59
 */
import Layout from '@/layout'

async function parseMenu(array) {
  const header = { path: '/', component: Layout }
  const privileges = array;
  const routes = []
  for (const privilege of privileges) {
    if (privilege.type === 'menu') {
      let route = {};
      if (privilege.children.length > 1) {
        route = {
          path: privilege.route,
          component: Layout,
          meta: { title: privilege.name, icon: privilege.icon, affix: true },
          redirect: `${privilege.route + privilege.children[0].route}`,
          children: []
        }
        if (privilege.children && privilege.children.length > 0) {
          for (const p of privilege.children) {
            const son_route = {
              path: p.route,
              component: function component(resolve) {
                require([`@/views${privilege.route + p.route}`], resolve)
              },
              name: p.route.replace(/\//g, '_'),
              meta: { title: p.name, icon: p.icon, affix: true },
              hidden: p.hidden == 1
            }
            route.children.push(son_route)
          }
        }
      } else {
        route = {
          path: privilege.route,
          component: Layout,
          redirect: `${privilege.route + privilege.children[0].route}`,
          children: []
        }
        if (privilege.children && privilege.children.length > 0) {
          for (const p of privilege.children) {
            const son_route = {
              path: p.route,
              component: function component(resolve) {
                require([`@/views${privilege.route + p.route}`], resolve)
              },
              name: p.route.replace(/\//g, '_'),
              meta: { title: p.name, icon: privilege.icon, affix: true },
              hidden: p.hidden == 1
            }
            route.children.push(son_route)
          }
        }
      }
      routes.push(route)
    }
  }
  routes.push({ path: '*', redirect: '/404', hidden: true })
  routes.unshift(header)
  return routes
}

export default parseMenu

